package com.demo.chapter14advcfunction;

import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.stream.Stream;

//函数柯里化
public class CurryDemo {

	//摄氏度到华氏度的转换：x*9/5+32
	//可理解为x*f+b，其中f是转换因子，b是基线调整因子
	//每次都需要传三个参数，而且容易出错
	public static double converter(double x, double f, double b){
		return x * f + b;
	}

	//柯里化，固定两个参数
	public static DoubleUnaryOperator converter(double f, double b){
		return (double x) -> x * f + b;
	}
	static DoubleUnaryOperator convertorCtoF = converter(9.0/5, 32);
	static DoubleUnaryOperator convertorUSDtoGBP = converter(0.6, 0);
	static DoubleUnaryOperator convertorKmtoMi = converter(0.6214, 0);
	
	static Function<Integer, Integer> multiplyCurry(Integer x){
		return y -> x * y;
	}

	public static void main(String[] args) {
		System.out.println(converter(1000, 9.0/5, 32));
		System.out.println(convertorCtoF.applyAsDouble(1000));
		Stream.of(1, 3, 5, 7).map(multiplyCurry(2)).forEach(System.out::println);
	}

}
